<?php
class Application_Model_Common
{
	private $objDb, $m_cache;
	private static $_instance = null;

	public function __construct()
	{
		$this->db = Zend_Registry::get('db');
		$this->cache = Zend_Registry::get('cache');
	}

	public static function getInstance()
	{
		if (null === self::$_instance) self::$_instance = new self();
		return self::$_instance;
	}

	public function getGroupIndustries($lang=LANGUAGE)
	{
		$query = $this->db->select();
		$query->from('industry_groups', array('*', 'group_name_'.$lang.' as group_name'));
		$query->order('group_column_job asc');
		$query->order('group_priority asc');
		return $query->query()->fetchAll();
	}

	/**
	 * @author	Ryan <nguyenhoaivien@yahoo.com>
	 * @date	Oc 16, 2013
	 * @param	language
	 			type: 1:all, 2:index, 3: industries on jsk home, 4: industries on emp home
	 * @return	array
	 * @desc	return list industries
	 */
	public function getIndustries($strLang=LANGUAGE, $type=2, $limit=999)
	{
		try 
		{
			$strCache = 'Model_Common_getIndustries_'.$strLang.'_'.$type.'_'.$limit;
			if(($arrResults = $this->cache->load($strCache))===FALSE)
			{
				$query = $this->db->select();
				$query->from('industries', array('*', 'industry_name_'.$strLang.' as industry_name'));
				if($type == 3) $query->order('home_jsk asc');
				$query->order('industry_priority asc');
				$query ->order('industry_name_'.$strLang.' asc');
				$query -> limit ($limit);
				$arrResults = $query->query()->fetchAll();
				if($type==2)
				{
					$arrTemp = array();
					foreach($arrResults as $R)
						$arrTemp[$R['INDUSTRY_ID']] = $R['INDUSTRY_NAME'];
					$arrResults = $arrTemp;
				}
				$this->cache->save($arrResults, $strCache,  array(MEMCACHE_PREF), DEFAULT_CACHE_DATE);
			}
			return $arrResults;
		}
		catch (Exception $e)
		{
			throw new Exception($e);
		}
	}

	/**
	 * @author  Ryan <nguyenhoaivien@yahoo.com>
	 * @date	Oc 16, 2013
	 * @param int Id, strLanguage
	 * @return string
	 * @desc return industry name
	 */
	public function getIndustry($ids, $strLang=LANGUAGE)
	{
		static $arrInd;
		if(!isset($arrInd)) $arrInd = $this->getIndustries($strLang, 2);
		$ret = array();
		if(!is_array($ids)) $ids = explode(',', $ids);
		foreach($ids as $id)
			if(isset($arrInd[$id])) $ret[] = $arrInd[$id];
		return implode(', ', $ret);
	}

	/**
	 * @author  Ryan <nguyenhoaivien@yahoo.com>
	 * @date	Oc 16, 2013
	 * @param string strLang, int type, int country_id
	 * @return array
	 * @desc return list locations
	 */
	public function getLocations($strLang=LANGUAGE, $type=2, $country_id=1, $limit=999)
	{
		try
		{
			$strCache = 'Model_Common_getLocations_'.$strLang.'_'.$type.'_'.($country_id == -1 ? 'all' : $country_id).'_'.$limit;
			if(($arrResults = $this->cache->load($strCache))===FALSE)
			{
				$query = $this->db->select();
				$query->from('locations', array('*', 'location_name_'.$strLang.' as location_name'));
				if($country_id > 0) $query->where('country_id = ?', $country_id);
				if($type == 3) $query->order('home_jsk asc');
				$arrResults = $query	->order('location_priority asc')
										->order('location_name_'.$strLang.' asc')
										->limit($limit)
										->query()->fetchAll();
				$arrTemp = array();
				if($type==2)
				{
					foreach($arrResults as $R)
						$arrTemp[$R['LOCATION_ID']] = $R['LOCATION_NAME'];
					$arrResults = $arrTemp;
				}
				$this->cache->save($arrResults, $strCache,  array(MEMCACHE_PREF), DEFAULT_CACHE_DATE);
			}
			return $arrResults;
		}
		catch (Exception $e)
		{
			throw new Exception($e);
		}
	}

	public function getGender($val=-1)
	{
		switch($val)
		{
			case 1: return _t('male');
			case 2: return _t('female');
			case -1: return array(1=>_t('male'), 2=>_t('female'));
		}
	}

	public function getMaterial($val=-1)
	{
		switch($val)
		{
			case 0: return _t('single');
			case 1: return _t('married');
			case -1: return array(1=>_t('single'), 2=>_t('married'));
		}
	}

	public function getSalary($val=NULL)
	{
		static $arrVals;
		if(!isset($arrVals)) $arrVals = array('ltt'=>_t('negotiable'), 'usd'=>'USD', 'vnd'=>'VND', 'lct'=>_t('salary_comp'));
		if($val===NULL) return $arrVals;
		return isset($arrVals[$val]) ? $arrVals[$val] : NULL;
	}
	/**
	 * @author  Ryan <nguyenhoaivien@yahoo.com>
	 * @date	Oc 16, 2013
	 * @param int id, string strLang
	 * @return string
	 * @desc: return location name
	 */
	public function getLocation($ids, $strLang=LANGUAGE)
	{
		static $arrLocs;
		if(!isset($arrLocs)) $arrLocs = $this->getLocations($strLang, 2, -1);
		$ret = array();
		if(!is_array($ids)) $ids = explode(',', $ids);
		foreach($ids as $id)
			if(isset($arrLocs[$id])) $ret[] = $arrLocs[$id];
		return implode(', ', $ret);
	}

	/**
	 * @author  Ryan <nguyenhoaivien@yahoo.com>
	 * @date	Oc 16, 2013
	 * @param int id, string strLang
	 * @return array or string
	 * @desc: return list country
	 */
	public function getCountries($id=NULL, $strLang=LANGUAGE)
	{
		try
		{
			static $arrResults;
			if(!isset($arrResults))
			{
				$strCache = 'Model_Common_getCountry_'.$strLang;
				if(($arrResults = $this->cache->load($strCache))===FALSE)
				{
					$arrResults = $this->db->select()
						->from('countries', array('country_id', 'country_name_'.$strLang.' as country_name'))
						->order('country_name_'.$strLang.' asc')
						->query()->fetchAll();
					$arrTemp = array();
					foreach($arrResults as $R)
						$arrTemp[$R['COUNTRY_ID']] = $R['COUNTRY_NAME'];
					$arrResults = $arrTemp;
					$this->cache->save($arrResults, $strCache,  array(MEMCACHE_PREF), DEFAULT_CACHE_DATE);
				}
			}
			if($id===NULL) return $arrResults;
			return isset($arrResults[$id]) ? $arrResults[$id] : NULL;
		}
		catch (Exception $e)
		{
			throw new Exception($e);
		}
	}

	/**
	 * @author  Ryan <nguyenhoaivien@yahoo.com>
	 * @date	Oc 16, 2013
	 * @param int id, string strLang
	 * @return array or string
	 * @desc: return list job level name
	 */
	public function getJobLevel($id=NULL, $strLang=LANGUAGE)
	{
		try
		{
			static $arrResults;
			if(!isset($arrResults))
			{
				$strCache = 'Model_Common_getJobLevel_'.$strLang;
				if(($arrResults = $this->cache->load($strCache))===FALSE)
				{
					$arrResults = $this->db->select()
										->from('levels', array('*', 'level_name_'.$strLang.' as  level_name'))
										->query()->fetchAll();
					$arrTemp = array();
					foreach($arrResults as $R)
						$arrTemp[$R['LEVEL_ID']] = $R['LEVEL_NAME'];
					$arrResults = $arrTemp;
					$this->cache->save($arrResults, $strCache,  array(MEMCACHE_PREF), DEFAULT_CACHE_DATE);
				}
			}
			if($id===NULL) return $arrResults;
			return isset($arrResults[$id]) ? $arrResults[$id] : NULL;
		}
		catch (Exception $e)
		{
			throw new Exception($e);
		}
	}
	
	public function getLevel($id=NULL, $strLang=LANGUAGE)
	{
		return $this->getJobLevel($id, $strLang);
	}

	/**
	 * @author  Ryan <nguyenhoaivien@yahoo.com>
	 * @date	Oc 16, 2013
	 * @param int id, string strLang
	 * @return array or string
	 * @desc: return list job degrees
	 */
	public function getJobDegree($id=NULL, $strLang=LANGUAGE, $type=2)
	{
		try
		{
			static $arrResults;
			if(!isset($arrResults))
			{
				$strCache = 'Model_Common_getJobDegree_'.$strLang;
				if(($arrResults = $this->cache->load($strCache))===FALSE)
				{
					$arrResults = $this->db->select()
							->from('degrees', array('*', 'degree_name_'.$strLang.' as degree_name'))
							->query()->fetchAll();
					if($type==2)
					{
						$arrTemp = array();
						foreach($arrResults as $R)
							$arrTemp[$R['DEGREE_ID']] = $R['DEGREE_NAME'];
						$arrResults = $arrTemp;
					}
					$this->cache->save($arrResults, $strCache,  array(MEMCACHE_PREF), DEFAULT_CACHE_DATE);
				}
			}
			if($id===NULL) return $arrResults;
			return isset($arrResults[$id]) ? $arrResults[$id] : NULL;
		}
		catch (Exception $e)
		{
			throw new Exception($e);
		}
	}

	public function getLanguages($id=NULL, $strLang=LANGUAGE, $type=2)
	{
		static $arrResults;
		if(!isset($arrResults))
		{
			$strCache = 'Model_Common_getLanguages_'.$strLang;
			if(($arrResults = $this->cache->load($strCache))===FALSE)
			{
				$arrResults = $this->db->select()
						->from('languages', array('*', 'language_name_'.$strLang.' as language_name'))
						->query()->fetchAll();
				if($type==2)
				{
					$arrTemp = array();
					foreach($arrResults as $R)
						$arrTemp[$R['LANGUAGE_ID']] = $R['LANGUAGE_NAME'];
					$arrResults = $arrTemp;
				}
				$this->cache->save($arrResults, $strCache,  array(MEMCACHE_PREF), DEFAULT_CACHE_DATE);
			}
		}
		if($id===NULL) return $arrResults;
		return isset($arrResults[$id]) ? $arrResults[$id] : NULL;
	}
	
	public function getlanguageLevels($id=NULL, $strLang=LANGUAGE, $type=2)
	{
		static $arrResults;
		if(!isset($arrResults))
		{
			$strCache = 'Model_Common_getlanguageLevels_'.$strLang;
			if(($arrResults = $this->cache->load($strCache))===FALSE)
			{
				$arrResults = $this->db->select()
						->from('language_levels', array('*', 'name_'.$strLang.' as name'))
						->query()->fetchAll();
				if($type==2)
				{
					$arrTemp = array();
					foreach($arrResults as $R)
						$arrTemp[$R['ID']] = $R['NAME'];
					$arrResults = $arrTemp;
				}
				$this->cache->save($arrResults, $strCache,  array(MEMCACHE_PREF), DEFAULT_CACHE_DATE);
			}
		}
		if($id===NULL) return $arrResults;
		return isset($arrResults[$id]) ? $arrResults[$id] : NULL;
	}

	public function render_quick_search_jobs($strLang=LANGUAGE)
	{
		$strCache = 'Model_Common_render_quick_search_jobs_'.$strLang;
	//	if(($strHtml = $this->cache->load($strCache))===FALSE)
		{
			$view = Zend_Registry::get('smarty');
			$view->arrIndustries = $this->getIndustries();
			$view->arrLocations = $this->getLocations();
			$strHtml = $view->fetch('partials/box.quicksearch.tpl');
			$this->cache->save($strHtml, $strCache,  array(MEMCACHE_PREF), DEFAULT_CACHE_TIME);
		}
		return $strHtml;
	}

	public function render_small_quick_search_jobs($strLang=LANGUAGE)
	{
		$strCache = 'Model_Common_render_small_quick_search_jobs_'.$strLang;
	//	if(($strHtml = $this->cache->load($strCache))===FALSE)
		{
			$view = Zend_Registry::get('smarty');
			$view->arrIndustries = $this->getIndustries();
			$view->arrLocations = $this->getLocations();
			$strHtml = $view->fetch('jobs/box.small_quicksearch.tpl');
			$this->cache->save($strHtml, $strCache,  array(MEMCACHE_PREF), DEFAULT_CACHE_TIME);
		}
		return $strHtml;
	}

	public function render_salary($unit, $salary_from, $salary_to)
	{
		if($unit == 'ltt') return _t('negotiable');
		if($unit == 'lct') return _t('salary_comp');

		$ret = ''; $extra = '';
		$l1 = '.'; $l2 = ',';
		$scale = 1; 
		if($unit=='vnd') { $l1 = ','; $l2 = '.'; $scale = 1000000; $extra = 'tr.'; }
		if($salary_from && !$salary_to)
		{
			$ret.= _t('over').' ';
			$ret.= number_format($salary_from/$scale, 0, $l1, $l2);
		}
		elseif(!$salary_from && !$salary_to)
		{
			$ret.= _t('under').' ';
			$ret.= number_format($salary_to/$scale, 0, $l1, $l2);
		}
		else
			$ret.= number_format($salary_from/$scale, 0, $l1, $l2).' - '.number_format($salary_to/$scale, 0, $l1, $l2);
		$ret.= ' '.$extra.strtoupper($unit);
		return $ret;
	}
}